<?php
// $Id$

/**
 * Controller_Mobile_Wap 控制器
 */
class Controller_Mobile_Wap extends Controller_Abstract
{
	private $aid = 0;
	private $acct = null;
	private $wapacct = null;
	function __construct($app)
	{
		parent::__construct($app);
		$this->import();
// 		ini_set("arg_separator.output","&amp;"); //用来产生 URL 参数的分割标志，默认是 "&"
// 		ini_set("session.use_cookies",0); //是否使用 cookies
// 		ini_set("session.use_trans_sid",1); //自动传递sessionid
// 		ini_set("session.name","sid");   //设置传递session的名字
// 		ini_set("url_rewriter.tags","a=href,go=href,area=href,frame=src,input=src,form=,fieldset=");    //标签重定义
		
	}
	function import()
	{
		if (!isset($_SESSION['acct']))
		{
			$this->aid = intval($this->_context->aid);
			
		}else{
			$this->aid = $_SESSION['acct']['agent_id'];
		}
		$this->acct = Acct::find('id = ?',$this->aid)->getOne();
		$this->wapacct = Acctwapset::find('acctid = ?',$this->aid)->getOne();
		$this->_view['acct'] = $this->acct;
		$this->_view['wapacct'] = $this->wapacct;
		$this->_view['aid'] = $this->aid;
		
		
	}
	function actionIndex()
	{
		if (isset($_REQUEST['md5']))
		{
			$md5 = trim($this->_context->md5);
			$md5 = explode('|', $md5);
			$result = '';
			if ($this->_context->isGET() )
			{
				$username = $md5[0];
				$userpass = $md5[1];
			
			
				$flag = Acct::check_user_pass($username,$userpass);
				if ($flag == false)
				{
					$result =  '账号密码错误';
				}else{
					$filename = Q::ini('app_config/ROOT_DIR').'/config/power.yaml';
					$set = Helper_YAML::load($filename);
					$power = $set['power'];
					if (isset($power[$_SESSION['acct']['acclevel']]))
					{
						$user_power = $power[$_SESSION['acct']['acclevel']];
						foreach ($user_power as $k=>$v)
						{
							$user_power[$k] = explode(',', $v);
						}
						$_SESSION['power'] = $user_power;
						Syslog::logs('登陆成功');
						$this->import();
					}else
					{
						$result = "对不起,您当前的用户组没有任何权限";
			
					}
			
					$result = "登陆成功";
					//return $this->_redirectMessage('登陆成功', '帐号登录成功', url('wap/ttindex'));
					//header('location:'.url('mobile::wap/index',array('aid'=>$_SESSION['acct']['agent_id'])));
				}
			}
		}
		
		#$this->_view['result'] = $result;
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
		
	}
	function actioncall()
	{
		$this->_checkLogin();
		$mobile = $_SESSION['acct']['acctname'];
		$pwd = $_SESSION['acct']['password'];
		$phone = $this->_context->phone;
	    $rnd  = $this->_context->rndcode;
			
			if (!Helper_Ckcode::check($rnd)){
				$result = '验证码错误';
				
			}else if(!Helper_Param::_phone($mobile))
		    {
			$result = '手机号码格式不正确'; //手机号码格式不正确
		    }else{
    			$user = Acctuser::checkAcctname($mobile);
    			if (!$user->id()){
    				$result = '不存在此绑定号码'; //不存在此绑定号码
    			}else if($user->password !== $pwd){
    				$result = '密码不正确' ;//密码不正确
    			}else{
    				$isone = Cbsubmit::find('bindname = ? and destination_number = ?',$mobile,$phone)->getCount('id');
    				if($isone){
    					$result = '存在此呼叫队列 请勿重复提交';//存在此呼叫队列 请勿重复提交
    				}else{
    					if(!Helper_Param::_phone($phone))
    					{
    						$result = '被叫号码格式不正确';//被叫号码格式不正确
    					}else{
    						Cbsubmit::call($mobile, $phone);
    						$result = '呼叫成功,请稍后';//提交成功
    					}
    		
    				}
			}
		}
		$this->_view['result'] = $result;
	}
	function actionlogin()
	{
		$result = '';
		if ($this->_context->isPOST() )
		{
			$username = $this->_context->post('username');
			$userpass = $this->_context->post('userpass');
			
			
			$flag = Acct::check_user_pass($username,$userpass);
			if ($flag == false)
			{
				$result =  '账号密码错误';
			}else{
				$filename = Q::ini('app_config/ROOT_DIR').'/config/power.yaml';
				$set = Helper_YAML::load($filename);
				$power = $set['power'];
				if (isset($power[$_SESSION['acct']['acclevel']]))
				{
					$user_power = $power[$_SESSION['acct']['acclevel']];
					foreach ($user_power as $k=>$v)
					{
						$user_power[$k] = explode(',', $v);
					}
					$_SESSION['power'] = $user_power;
					Syslog::logs('登陆成功');
				}else
				{
					$result = "对不起,您当前的用户组没有任何权限";
					
				}
				
				$result = "登陆成功";
				//return $this->_redirectMessage('登陆成功', '帐号登录成功', url('wap/ttindex'));
				header('location:'.url('mobile::wap/index',array('md5'=>$_SESSION['acct']['acctname'].'|'.$_SESSION['acct']['password'])));
			}
		}
		$this->_view['result'] = $result;
	}
	function actionnoreg()
	{
		$result = '';
		if($this->_context->isPOST())
		{
			if (false)
			{
				$result = '';
			}else{
				$result = '';
				$payid = trim($this->_context->payid);
				$mobile = trim($this->_context->adphone);
				$unionuser = trim($this->_context->unionuser);
				if (strlen($payid)<2 || strlen($mobile)<2 )
				{
					$result = '必须输入数据';
					
				}
				$isNew = Acctuser::find('acctname = ?',$mobile)->getCount('id');
				if ($isNew>0)
				{
					$result = '快捷开户只能开卡不能给现有用户充值！';
				}
				$card = Vouchercard::find('payid = ? and enable = 1 and vouchered = 0',$payid)->getOne();
				if (!$card->id())
				{
					$result = '无快捷卡可以使用!';
				}
				if ($result)
				{
					exit($result);
				}
				$cardid = $card->cardname;
				$cardpwd = $card->password;
				if(!Helper_Param::_call($mobile))
				{
					$result = '手机号码格式不正确';
				}else{
					$flag = Acctuser::use_card($mobile, $cardid, $cardpwd,'',$unionuser);
					if($flag === true)
					{
						//$card = Vouchercard::find('cardname = ? and password = ?',$cardid,$cardpwd)->setColumns('id,balance')->getOne();
						$result = '开户成功,余额:'.$card->balance;
					}else{
						
						$result = '失败';
					}
		
				}
		
			}
		
		}else{
		
		}
		$this->_view['result'] = $result;
	}
	function actiondownload()
	{
		//dump($_SERVER);die;
		$server = $_SERVER['SERVER_ADDR'];
		$server = explode('.', $server);
		$this->_checkLogin();
		if($this->aid)
		{
			$wapset = $this->wapacct;
			header('location: http://download.cr10000.net:8088/download/'.$server[count($server)-1].'.apk');
		}
	}
	function actionnopass()
	{
		$md5 = trim($this->_context->md5);
		$md5 = explode('|', $md5);
		$result = '';
		if ($this->_context->isGET() )
		{
			$username = $md5[0];
			$userpass = $md5[1];
			
			
			$flag = Acct::check_user_pass($username,$userpass);
			if ($flag == false)
			{
				$result =  '账号密码错误';
			}else{
				$filename = Q::ini('app_config/ROOT_DIR').'/config/power.yaml';
				$set = Helper_YAML::load($filename);
				$power = $set['power'];
				if (isset($power[$_SESSION['acct']['acclevel']]))
				{
					$user_power = $power[$_SESSION['acct']['acclevel']];
					foreach ($user_power as $k=>$v)
					{
						$user_power[$k] = explode(',', $v);
					}
					$_SESSION['power'] = $user_power;
					Syslog::logs('登陆成功');
				}else
				{
					$result = "对不起,您当前的用户组没有任何权限";
					
				}
				
				$result = "登陆成功";
				//return $this->_redirectMessage('登陆成功', '帐号登录成功', url('wap/ttindex'));
				//header('location:'.url('mobile::wap/index',array('aid'=>$_SESSION['acct']['agent_id'])));
			}
		}
		$this->_view['result'] = $result;
	}
	function actiontxlindex()
	{
		$this->_checkLogin();
		$data = Cbnumlist::find('agent_id = 0')->order('sipcode asc')->getAll();
		$this->_view['data'] = $data;
	}
	function _checkLogin()
	{
		if(!isset($_SESSION['acct']['id']))
		{
			header('location:'.url('mobile::wap/index'));
			die;
		}
	}
	function actionchangename()
	{
		$this->_checkLogin();
		$result = '';
		if($this->_context->isPOST())
		{
			$newname = trim($this->_context->newname);
			$rnd  = $this->_context->rndcode;
			
			if (!Helper_Ckcode::check($rnd)){
				$result = '验证码错误';
				
			}else if(!Helper_Param::_call($newname))
			{
				$result = '新号码格式错误';
			}else{
				$acctname = trim($_SESSION['acct']['acctname']);
				if (!$newname){
					$result = '新号码格式错误';
				}else{
					$checknew = Acctuser::checkAcctname($newname);
					if ($checknew->id())
					{
						$result = '新号码已经被使用';
					}else{
						$acctuser = Acctuser::find('acctname = ?',$acctname)->getOne();
						if (!$acctuser->id())
						{
							$result = '被修改用户不存在';
						}else{
							User_Safelog::log($acctuser->id, 1, 0,$acctuser->acctname,$newname);
							$acctuser->acctname = $newname;
							$acctuser->save(0,'update');
							Syslog::notloginlogs("ID:{$acctuser->id} {$acctname} 修改帐号为:{$newname}");
							$result = '修改成功,请使用新账号登陆<a href="/mobile/wap/login" >登陆</a>';
							if(isset($_SESSION['acct']))unset($_SESSION['acct']);
							//return $this->_redirectMessage('修改成功', $result, '/');
						}
					}
					
				}
			}
		}else{
			
		}
		$this->_view['result'] = $result;
	}
}


